<template>
  <el-dialog v-el-drag-dialog :title="!form.attrGroupId?'新建属性分组':'编辑属性分组'" :visible.sync="visible" :append-to-body="appendToBody" :before-close="onClose">
    <el-form ref="groupForm" :model="form" :rules="rules" label-width="120px" :disabled="!hasLoaded">
      <el-form-item hidden>
        <el-input v-model="form.attrGroupId" hidden />
      </el-form-item>

      <el-card>
        <el-form-item label="组名" prop="attrGroupName">
          <el-input v-model="form.attrGroupName" />
        </el-form-item>
        <el-form-item label="图标" prop="icon">
          <div style="display:flex;">
            <icon-picker v-model="form.icon" style="width:90%" />
            <i class="el-icon-delete" @click="form.icon=''" />
          </div>
        </el-form-item>
        <el-form-item label="排序" prop="groupSort">
          <el-input-number v-model="form.groupSort" :min="1" />
        </el-form-item>
        <el-form-item label="所属分类" prop="catalogPath">
          <el-cascader
            v-model="form.catalogPath"
            filterable
            clearable
            placeholder="试试搜索：手机"
            :options="categoryData"
            :props="props"
            @change="handleChangeCatalog"
          />
        </el-form-item>
        <el-form-item label="描述" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            :rows="3"
          />
        </el-form-item>
      </el-card>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button v-if="hasLoaded" type="primary" icon="el-icon-check" @click="onSubmit">确 定</el-button>
      <el-button icon="el-icon-close" @click="onClose">关 闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { groupLoad, groupSave } from '@/api/mall/product/attrgroup'
import { categoryTreeData } from '@/api/mall/product/category'
import IconPicker from '@/components/IconPicker'

export default {
  name: 'AttrGroupFormDialog',
  components: {
    IconPicker
  },
  data() {
    return {
      visible: false,
      appendToBody: false,
      hasLoaded: false,
      form: {
        attrGroupId: undefined,
        attrGroupName: undefined,
        icon: '',
        groupSort: 1,
        catalogId: undefined,
        catalogPath: [],
        remark: undefined
      },
      rules: {
        attrGroupName: [{ required: true, trigger: 'blur', message: '组名不能为空' }],
        catalogPath: [{ required: true, trigger: 'blur', message: '所属分类不能为空' }]
      },
      props: {
        value: 'id',
        label: 'name'
      },
      categoryData: []
    }
  },
  methods: {
    init(groupId, catalogId) {
      this.hasLoaded = false
      this.form.categoryData = []
      this.form.attrGroupId = groupId || null
      this.form.catalogId = catalogId || null
      this.visible = true
      this.$nextTick(() => {
        this.getCategoryData()
        this.$refs.groupForm.resetFields()
        this.loadForm(groupId, catalogId)
      })
    },
    loadForm(groupId, catalogId) {
      groupLoad({
        'groupId': groupId,
        'catalogId': catalogId
      }).then(response => {
        this.form = this.$api.objectUpdate(this.form, response.param)
        // 判断当前用户权限
        this.hasLoaded = this.$perm.hasPermissionOr(['mall:product:attr'])
      }).catch(() => {})
    },
    getCategoryData() {
      categoryTreeData({
        from: 'vue',
        excludeRoot: 'true'
      }).then(response => {
        this.categoryData = response
      }).catch(() => {})
    },
    handleChangeCatalog(value) {
      this.form.catalogId = value[value.length - 1]
    },
    onSubmit() {
      this.$refs.groupForm.validate(valid => {
        if (valid) {
          groupSave(this.form).then(response => {
            this.$modal.msgSuccess(response.msg)
            this.$emit('confirmForm')
            this.$refs.groupForm.resetFields()
            this.visible = false
          }).catch(() => {})
        }
      })
    },
    onClose() {
      this.form.catalogPath = []
      this.$refs.groupForm.resetFields()
      this.visible = false
    }
  }
}
</script>
